package com.isea.pomodoro;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicInteger;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

@SuppressWarnings("serial")
public class TimePanel extends JPanel{
	private JButton startBtn;
	private JButton stopBtn;
	private JButton resetBtn;
	private JLabel timeLabel;
	private TrayIcon icon;
	private String name;

	private Timer timer;
	
	private final int totalTime;
	private AtomicInteger currTime;
	private boolean isStart = false;
	private boolean isStop = false;
	
	public TimePanel(String title,final int seconds,TrayIcon icon) {
		super();
		
		totalTime = seconds;
		this.icon = icon;
		this.name = title;
		currTime = new AtomicInteger(seconds);
		
		setBackground(PomodoroGui.bgColor);
		setLayout(new BorderLayout(0, 0));
		
		timeLabel = new JLabel("00:00");
		timeLabel.setFont(new Font("SansSerif", Font.BOLD, 80));
		timeLabel.setHorizontalAlignment(SwingConstants.CENTER);
		add(timeLabel, BorderLayout.CENTER);
		
		JPanel btnPanel = new JPanel();
		btnPanel.setBackground(PomodoroGui.bgColor);
		btnPanel.setPreferredSize(new Dimension(10, 60));
		add(btnPanel, BorderLayout.SOUTH);
		btnPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		startBtn = new JButton("开始");
		startBtn.addActionListener(startActionListener);
		startBtn.setForeground(new Color(255, 255, 255));
		startBtn.setFont(new Font("微软雅黑", Font.BOLD, 14));
		startBtn.setBackground(new Color(34, 139, 34));
		startBtn.setPreferredSize(new Dimension(80, 50));
		btnPanel.add(startBtn);
		
		stopBtn = new JButton("暂停");
		stopBtn.addActionListener(stopActionListener);
		stopBtn.setFont(new Font("微软雅黑", Font.BOLD, 14));
		stopBtn.setForeground(new Color(255, 255, 255));
		stopBtn.setBackground(new Color(153, 0, 0));
		stopBtn.setPreferredSize(new Dimension(80, 50));
		btnPanel.add(stopBtn);
		
		resetBtn = new JButton("重置");
		resetBtn.addActionListener(resetActionListener);
		resetBtn.setBackground(new Color(255, 250, 205));
		resetBtn.setFont(new Font("微软雅黑", Font.BOLD, 14));
		resetBtn.setPreferredSize(new Dimension(80, 50));
		btnPanel.add(resetBtn);
		//初始化
		timeLabel.setText(TimeUtil.getTime(totalTime));
	}
	
	/**
	 * 开始
	 */
	public void start(){
		if(!isStart){
			isStart = true;
			isStop = false;
			timer = new Timer(true);
			timer.schedule(getTask(), 0,1000);
		}
	}
	
	/**
	 * 重置
	 */
	public void reset(){
		if(timer!=null){
			isStart = false;
			isStop = true;
			timer.cancel();
			timeLabel.setText(TimeUtil.getTime(totalTime));
			currTime.set(totalTime);
		}
	}
	
	private TimerTask getTask(){
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				synchronized (currTime) {
					if(currTime.get()>=0){
						timeLabel.setText(TimeUtil.getTime(currTime.get()));
						if(icon!=null){
							icon.setToolTip(name + "："+timeLabel.getText());
						}
						currTime.decrementAndGet();
					}
					else {
						JOptionPane.showMessageDialog(TimePanel.this, name + "时间到!");
						this.cancel();
					}
				}
			}
		};
		return task;
	}

	private ActionListener startActionListener = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			start();
		}		
	};
	
	private ActionListener stopActionListener = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			if(!isStop){
				isStop = true;
				isStart = false;
				timer.cancel();
			}
		}
	};
	
	private ActionListener resetActionListener = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			reset();
		}
	};
}
